﻿Imports System.Linq
Imports System.Collections.Generic
Imports SISSAT.BusinessLayer.Managers
Imports SISSAT.BusinessLayer.Services
Imports SISSAT.BusinessLayer.DataViews
Imports SISSAT.BusinessLayer.DomainModels

Namespace Views
    Public Class SubjectListControlPresenter

        Private Property View As ISubjectListControlView
        Private Property Service As IScheduleService
        Private Property MatriculationService As IMatriculationService
        Private Property InstructorId As Integer

        Public Sub New(view As ISubjectListControlView)
            Service = New ScheduleService
            MatriculationService = New MatriculationService

            Me.View = view
            AddHandler Me.View.OnInitialization, AddressOf OnInitialization
            AddHandler Me.View.OnSelectedIndexChange, AddressOf OnSelectedIndexChange

            Dim employeeService As IEmployeeService = New EmployeeService
            Dim employee As Employee = employeeService.ReadByCode(Me.View.EmployeeCode)
            Me.InstructorId = employee.EmployeeId
        End Sub

        Private Sub OnInitialization()
            Dim entities As IEnumerable(Of Schedule) = Service.ReadAllByInstructorId(Me.InstructorId)

            Dim listItems As IEnumerable(Of KeyValuePair(Of String, String)) =
                From entity In entities
                Let syKey As String = String.Format("{0}|{1}", entity.SchoolYear, entity.Semester)
                Let syValue As String = String.Format("SY {0}/{1} Semester", entity.SchoolYear, entity.Semester)
                Order By entity.SchoolYear Descending, entity.Semester Descending
                Select New KeyValuePair(Of String, String)(syKey, syValue)

            Me.View.DropDownListDataSource = listItems.Distinct()
        End Sub

        Private Sub OnSelectedIndexChange()
            If String.IsNullOrEmpty(Me.View.SelectedValue) Then
                Return
            End If

            Dim delimiter() As Char = {CType("|", Char)}
            Dim selectedValue As String() = Me.View.SelectedValue.Split(delimiter)
            Dim schoolYear As String = selectedValue(0)
            Dim semester As Semesters = CType([Enum].Parse(GetType(Semesters), selectedValue(1), True), Semesters)

            Dim entities As IEnumerable(Of Schedule) = _
                Service.ReadAllByInstructorId(Me.InstructorId, schoolYear, semester)

            Me.View.RepeaterDataSource = From entity In entities
                                         Let count As Integer = MatriculationService.ReadAllByScheduleId(entity.ScheduleId).Count()
                                         Where count > 0
                                         Order By entity.StartTime Ascending
                                         Select New SubjectListDataView With {
                                             .Day = entity.ScheduleDay,
                                             .RoomNumber = entity.RoomNumber,
                                             .ScheduleId = entity.ScheduleId,
                                             .SubjectCode = entity.Subject.Code,
                                             .SubjectName = entity.Subject.Name,
                                             .Time = entity.ScheduleTime
                                         }
        End Sub

    End Class
End Namespace

